import Vue from 'vue'
import VueRouter from 'vue-router'
const Profile = () => import('../components/user/Profile.vue')
const UserList = () => import('../components/user/UserList.vue')
Vue.use(VueRouter)



const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/',
    name: 'admin',
    component: () => import('../views/Admin.vue'),
    redirect: { name: 'index' },
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('../components/admin/Index.vue'),
      },
      {
        path: 'articleList',
        name: 'articleList',
        component: () => import('../components/article/ArticleList.vue'),
      },
      {
        path: 'addArticle/:id',
        name: 'addArticle',
        props: true,
        component: () => import('../components/article/AddArticle.vue'),
      },
      {
        path: 'categoryList',
        name: 'categoryList',
        component: () => import('../components/category/CategoryList.vue'),
      },
      {
        path: 'userList',
        name: 'userList',
        component: UserList,
      },
      {
        path: 'profile/:id',
        name: 'profile',
        component: Profile,
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 用户全局身份认证
router.beforeEach((to, from, next) => {
  const token = window.sessionStorage.getItem("token")
  // 检查token
  if (to.name != 'login' && !token) {
    next({ name: 'login' })
  } else {
    next()
  }
})
export default router
